package GUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JTextField;
import javax.swing.JButton;

import Client.BusinessImplements;
import Controler.Add_new_Author_controler;
import Controler.Update_Author_Controler;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;
/**
 * @author Lucie
 * Class name  Update_Author_Panel_sec
 * extend  Abstract_Jpanel
 * purpose: change the name of the item that the user chose
 */
public class Update_Author_Panel_sec extends Abstract_Jpanel {

	/**
	 * Create the panel.
	 */ 
	private Update_Author_Panel_first up_author_first;
	private JTextField textFieldname;
	private String author_id;
	private String author_name;
	public Update_Author_Panel_sec() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		up_author_first=new  Update_Author_Panel_first();
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(10, 51, 597, 257);
		add(panel);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(SystemColor.control, 6));
		panel_1.setBackground(new Color(220, 220, 220));
		panel_1.setBounds(65, 40, 445, 167);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblAuthorName = new JLabel("author name:");
		lblAuthorName.setBounds(39, 26, 88, 27);
		panel_1.add(lblAuthorName);
		lblAuthorName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		textFieldname = new JTextField();
		textFieldname.setBounds(151, 31, 176, 20);
		panel_1.add(textFieldname);
		textFieldname.setColumns(10);
		
		JLabel lblEnterNewName = new JLabel("enter new name for the author");
		lblEnterNewName.setBounds(68, 72, 256, 20);
		panel_1.add(lblEnterNewName);
		lblEnterNewName.setForeground(Color.RED);
		lblEnterNewName.setFont(new Font("Segoe Print", Font.PLAIN, 13));
		
		JLabel lblUpdateAuthorDeatails = new JLabel("Update Author Details");
		lblUpdateAuthorDeatails.setForeground(new Color(25, 25, 112));
		lblUpdateAuthorDeatails.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblUpdateAuthorDeatails.setBounds(10, 11, 415, 31);
		add(lblUpdateAuthorDeatails);
		//btnGo; call the controller of this panel to change the name of the author
		JButton btnGo = new JButton("Update");
		btnGo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
	
				Update_Author_Controler update_author_controler=new Update_Author_Controler();
				
				if(update_author_controler.set_new_auther( author_id,get_name()))
				{
					next_screen_success();
					empty_text_fields();
					
				}
				else {
					next_screen_faild(update_author_controler.get_msg());
					
				}
	
			}
		});
		btnGo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnGo.setBounds(484, 319, 102, 42);
		add(btnGo);
		//btnBack go book to Update_Author_Panel_first
		JButton btnBack = new JButton("back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(up_author_first);
				up_author_first.setSize(getMaximumSize());
				up_author_first.on_screen_load();
				
				
			}
		});
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnBack.setBounds(40, 319, 102, 42);
		add(btnBack);
		 
		
	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void on_screen_load() {
		
		
	}
	/**
	 * 
	 * @param id
	 * @param name
	 *  this method is the first method we call in this screen
	 * the main purpose of this method is to initialize the text fields 
	 */
	public void on_screen_load(String id,String name)
	{
	
		author_id=id;
		author_name=name;
		textFieldname.setText(author_name);
		
	}

	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 
	 * @return name of the author
	 */
	public String get_name()
	{
		return textFieldname.getText();
		
	}
	/**
	 *  this method display showMessageDialog with a success msg
	 */
	public void next_screen_success()
	{
		JOptionPane.showMessageDialog(this,"apply success");
	}
	/**
	 * this method display showMessageDialog with a failed msg
	 * @param msg
	 */
	public void next_screen_faild(String msg)
	{
		JOptionPane.showMessageDialog(this,"apply faild "+msg);
	}
	/**
	 * empty text fields
	 */
	public void empty_text_fields()
	{
		textFieldname.setText(null);
		
	}
	
	

}
